// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#ifndef DEVICEFINDER_H
#define DEVICEFINDER_H

#include "bluetoothbaseclass.h"

#include <QBluetoothDeviceDiscoveryAgent>
#include <QBluetoothDeviceInfo>

#include <qqml.h>

#include <QTimer>
#include <QVariant>

class DeviceInfo;
class DeviceHandler;

class DeviceFinder: public BluetoothBaseClass
{
    Q_OBJECT

    Q_PROPERTY(bool scanning READ scanning NOTIFY scanningChanged)
    Q_PROPERTY(QVariant devices READ devices NOTIFY devicesChanged)     //更新Connect.qml中的列表。
    Q_PROPERTY(QVariant bin_list READ get_bin_list NOTIFY binsChanged)
    Q_PROPERTY(QString bin_dir READ get_bin_dir CONSTANT);
    Q_PROPERTY(QString  bin_name READ get_bin_name WRITE set_bin_name NOTIFY bin_nameChanged);

    QML_ELEMENT //QQ

public:
    DeviceFinder(DeviceHandler *handler, QObject *parent = nullptr);
    ~DeviceFinder();

    bool scanning() const;
    QVariant devices();
    QVariant get_bin_list();
    QString get_bin_dir();
    QString get_bin_name();
    void set_bin_name(QString name);

public slots:
    void startSearch();
    void connectToService(const QString &address);
    void openfile(quint32 idx);

private slots:
    void addDevice(const QBluetoothDeviceInfo&);
    void scanError(QBluetoothDeviceDiscoveryAgent::Error error);
    void scanFinished();
    void onBin_nameChanged();

signals:
    void scanningChanged();         //扫描状态有变。
    void devicesChanged();          //设备列表有变。QQ需要显式定义吗？
    void binsChanged();             //bin文件列表变化时，发送本信号。
    void bin_nameChanged();         //文件名更改时更新


private:
    DeviceHandler *m_deviceHandler;
    QBluetoothDeviceDiscoveryAgent *m_deviceDiscoveryAgent;
    QList<QObject*> m_devices;

    QTimer m_demoTimer;

public:
    QStringList bin_list;
    QString bin_dir;
    QString bin_name;
};

#endif // DEVICEFINDER_H
